package com.sgb.knowledgeshare.view.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.appbar.AppBarLayout;
import com.sgb.knowledgeshare.R;
import com.sgb.knowledgeshare.databinding.FragmentShareMineBinding;
import com.sgb.knowledgeshare.entity.ShareUserInfoEntity;
import com.sgb.knowledgeshare.event.shareLeaseEvent;
import com.sgb.knowledgeshare.view.activity.ShareMinFansActivity;
import com.sgb.knowledgeshare.view.activity.ShareUserVideoEditActivity;
import com.sgb.knowledgeshare.viewmodel.ShareMineModel;
import com.swgk.core.base.AppFragment;
import com.swgk.core.event.EventConstant;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.FragPagerAdapter;
import com.swgk.core.util.CommonUtils;
import com.swgk.core.util.SharedPreferenceUtil;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * 分享-我的
 */
public class ShareMineFragment extends AppFragment {
    ShareMineModel viewmode;
    public static final int USER_PRODUCTION_TYPE = 0;//作品
    public static final int USER_LIKE_TYPE = 1;//喜欢
    private FragmentShareMineBinding binding;
    private ArrayList<Fragment> mFragments;
    private int curpos = -1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_share_mine, container, false);
        viewmode = new ViewModelProvider(this).get(ShareMineModel.class);
        viewmode.setActivity(ShareMineFragment.this);
        View decor = getActivity().getWindow().getDecorView();
        decor.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        binding.llShareUserLike.setOnClickListener(this);//获赞
        binding.llShareUserAttention.setOnClickListener(this);//关注
        binding.llShareUserFans.setOnClickListener(this);//粉丝
        binding.userEditVideo.setOnClickListener(this);//删除视频
        binding.setViewModel(viewmode);
        return binding.getRoot();
    }

    @Override
    public void onResume() {
        super.onResume();
        viewmode.getUserHomePage(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        binding.appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                //verticalOffset始终为0以下的负数
                float percent = (Math.abs(verticalOffset * 1.0f) / appBarLayout.getTotalScrollRange());
                binding.toolbar.setBackgroundColor(changeAlpha(Color.WHITE, percent));
            }
        });
        super.onActivityCreated(savedInstanceState);
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }


    /**
     * 根据百分比改变颜色透明度
     */
    public int changeAlpha(int color, float fraction) {
        int red = Color.red(color);
        int green = Color.green(color);
        int blue = Color.blue(color);
        int alpha = (int) (Color.alpha(color) * fraction);
        return Color.argb(alpha, red, green, blue);
    }

    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void onEvent(shareLeaseEvent event) {
        if (event.getMessageTag() == EventConstant.EVENT_DELECT_VIDEO) {
            int messageData = (int) event.getMessageData();
            if (messageData == 2) {
                viewmode.getUserHomePage(SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.USER_NO, ""));
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();//点赞
        if (id == R.id.ll_share_user_like) {
        } else if (id == R.id.ll_share_user_attention) {//关注
            if (CommonUtils.buttonClick()) {
                return;
            } else {
                ShareMinFansActivity.start(getContext(), 0);
            }
        } else if (id == R.id.ll_share_user_fans) {//粉丝
            if (CommonUtils.buttonClick()) {
                return;
            } else {
                ShareMinFansActivity.start(getContext(), 1);
            }
        } else if (id == R.id.user_edit_video) {//删除视频
            if (CommonUtils.buttonClick()) {
                return;
            } else {
                ShareUserVideoEditActivity.start(getContext());//编辑视频类
            }
        }
    }

    private void initview(ShareUserInfoEntity data) {
        String[] titles = {"作品  " + data.getCreateCount(), "喜欢  " + data.getLikeCount()};
        mFragments = new ArrayList<>();
        mFragments.add(ShareMinUserListFragment.newInstance(data.getUserNo(), USER_PRODUCTION_TYPE));
        mFragments.add(ShareMinUserListFragment.newInstance(data.getUserNo(), USER_LIKE_TYPE));
        FragPagerAdapter mAdapetr = new FragPagerAdapter(getChildFragmentManager(), mFragments);
        binding.viewpager.setAdapter(mAdapetr);
        binding.tablayout.setViewPager(binding.viewpager, titles);
        binding.tablayout.setCurrentTab(0);
        binding.tablayout.notifyDataSetChanged();

    }

    //用户信息数据
    public void setUserData(ShareUserInfoEntity data) {
        if (data != null) {
//        头像
            if (!TextUtils.isEmpty(data.getPhoto())) {
                Glide.with(getContext()).load(data.getPhoto() + "")
                        .apply(RequestOptions.bitmapTransform(new CircleCrop()).placeholder(R.mipmap.share_def_photo))
                        .into(binding.shareUserLogo);
            }
//        用户名称
            if (!TextUtils.isEmpty(data.getName())) {
                binding.shareUserName.setText(data.getName());
            } else {
                binding.shareUserName.setText("");
            }
            //公司名称
            if (!TextUtils.isEmpty(data.getCompanyName())) {
                binding.shareUserCompany.setText(data.getCompanyName());
            } else {
                binding.shareUserCompany.setText("");
            }
            DecimalFormat df = new DecimalFormat("##0.0");
            //获赞数量
            if (data.getLaudCount() > 9999) {
                double commentnum = data.getLaudCount() / 10000;
                binding.shareUserLike.setText(df.format(commentnum) + "w");
            } else {
                binding.shareUserLike.setText(data.getLaudCount() + "");
            }
            //关注数量
            if (data.getFollow() > 9999) {
                double Follow = data.getFollow() / 10000;
                binding.shareUserAttention.setText(df.format(Follow) + "w");
            } else {
                binding.shareUserAttention.setText(data.getFollow() + "");
            }
            //粉丝数量
            if (data.getFens() > 9999) {
                double Fensnum = data.getFens() / 10000;
                binding.shareUserFans.setText(df.format(Fensnum) + "w");
            } else {
                binding.shareUserFans.setText(data.getFens() + "");
            }
            binding.viewpager.setVisibility(View.VISIBLE);
            initview(data);
        } else {
            binding.viewpager.setVisibility(View.GONE);
        }
    }
}
